package com.mooral.web.util;

import java.util.ArrayList;
import java.util.HashMap;

// TODO Armar en funcion de un archivo XML

public class SlugUtils {
	static HashMap<String, String> actionSlugs = new HashMap<String, String>();
	static HashMap<String, HashMap<String, String>> actionSlugsParameters = new HashMap<String, HashMap<String, String>>();
	
	static{
		actionSlugs.put("/voucherSearch", "/search/voucher");
		actionSlugs.put("/voucherSearchByCategory", "/voucher/search/by-category");
		actionSlugs.put("/merchantView", "/merchant");
		actionSlugs.put("/voucherSearchMostPopular", "/voucher/search/most-popular");
		actionSlugs.put("/voucherSearchExclusiveDeals", "/voucher/search/exclusive-deals");
		actionSlugs.put("/voucherSearchHotToday", "/voucher/search/hot-today");
		actionSlugs.put("/voucherSearchNewDiscounts", "/voucher/search/new-discounts");
		actionSlugs.put("/voucherSearchExpiringDeals", "/voucher/search/expiring-deals");
		
		HashMap<String, String> params = null;
		
		params = new HashMap<String, String>();
		params.put("#1#", "/#1#");
		params.put("#2#", "/#2#");
		actionSlugsParameters.put("/voucherSearch", params);
		
		params = new HashMap<String, String>();
		params.put("#1#", "/#1#");
		params.put("#2#", "/#2#");
		actionSlugsParameters.put("/voucherSearchByCategory", params);
		
		params = new HashMap<String, String>();
		params.put("#1#", "/#1#");
		params.put("#2#", "/#2#");
		actionSlugsParameters.put("/merchantView", params);
		
		params = new HashMap<String, String>();
		params.put("#1#", "/#1#");
		actionSlugsParameters.put("/voucherSearchMostPopular", params);		
		
		params = new HashMap<String, String>();
		params.put("#1#", "/#1#");
		actionSlugsParameters.put("/voucherSearchExclusiveDeals", params);		
		
		params = new HashMap<String, String>();
		params.put("#1#", "/#1#");
		actionSlugsParameters.put("/voucherSearchHotToday", params);		
		
		params = new HashMap<String, String>();
		params.put("#1#", "/#1#");
		actionSlugsParameters.put("/voucherSearchNewDiscounts", params);		
		
		params = new HashMap<String, String>();
		params.put("#1#", "/#1#");
		actionSlugsParameters.put("/voucherSearchExpiringDeals", params);		
	}
	
	public String getActionSlug(String actionName){
		return getActionSlug(actionName,null);
	}

	public boolean isActionSlugMapped(String actionName){
		return ( actionSlugs.get(actionName) == null? false:true);
	}
	
	public String getActionSlug(String actionName,String[] params){
		String slug = actionSlugs.get(actionName);
		
		if(slug == null) return actionName;
		
		if(params!=null){
			
			HashMap<String, String> actionParameters = actionSlugsParameters.get(actionName);
			
			if(actionParameters != null){
				for (int i = 0; i < params.length; i++){
					String param = actionParameters.get("#" + (i +1) + "#");
					slug += param.replaceAll("#" + (i +1) + "#", params[i]);
				}
			}
			
		}

		return slug;
	}
}
